<template>
	<view class="content">
		<button class="size" v-for="(site,key) in list" :key="key" @tap="handel(site,key)">{{site}}</button>
		<view>{{msg}}</view>
		<button class="size" @tap="requestFileSystem">请求本地文件系统对象</button>
		<button class="size" @tap="resolveLocalFileSystemURL">通过URL参数获取目录对象或文件对象</button>
		<button class="size" @tap="convertLocalFileSystemURL">将本地URL路径转换成平台绝对路径</button>
		<button class="size" @tap="convertAbsoluteFileSystem">将平台绝对路径转换成本地URL路径</button>
		<button class="size" @tap="getAudioInfo">获取音频文件信息</button>
		<button class="size" @tap="getImageInfo">获取图片信息</button>
		<button class="size" @tap="getVideoInfo">获取视频文件信息</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgUrl: "",
				channels: [],
				list: {
					"PRIVATE_WWW": '应用私有资源目录',
					"PRIVATE_DOC": "应用私有文档目录",
					"PUBLIC_DOCUMENTS": "应用公共文档目录",
					"PUBLIC_DOWNLOADS": '应用公共下载目录'
				},
				msg: ''
			}
		},
		onLoad() {

		},
		methods: {
			handel(site, key) {
				this.msg = key;

			},
			requestFileSystem() {
				// 请求本地文件系统对象
				plus.io.requestFileSystem(
				  plus.io.PUBLIC_DOCUMENTS,  // 文件系统中的根目录
				  fs => {
				    // 创建或打开文件, fs.root是根目录操作对象,直接fs表示当前操作对象
				    fs.root.getFile('test.json', {
				      create: true  // 文件不存在则创建
				    }, fileEntry => {
				      // 文件在手机中的路径
				      console.log(fileEntry.fullPath)
				      fileEntry.createWriter(writer => {
				        // 写入文件成功完成的回调函数
				        writer.onwrite = e => {
				          plus.nativeUI.alert("写入数据成功");
				        };
				        // 写入数据
				        writer.write(JSON.stringify({data:['1','2']}));
				      })
				    }, e => {
				     plus.nativeUI.alert("getFile failed: " + e.message);
				    });
				  },
				  e => {
				   plus.nativeUI.alert(e.message);
				  }
				);

			},
			resolveLocalFileSystemURL() {
				plus.io.resolveLocalFileSystemURL("/var/mobile/Containers/Data/Application/C4B37D22-B29B-4242-B400-1EBAF733BD28/Documents/Pandora/documents/test.json", function(entry) {
					// 可通过entry对象操作test.html文件 
					entry.file(function(file) {
						var fileReader = new plus.io.FileReader();
						plus.nativeUI.alert("getFile:" + JSON.stringify(file));
						fileReader.readAsText(file, 'utf-8');
						fileReader.onloadend = function(evt) {
							plus.nativeUI.alert("11" + evt);
							plus.nativeUI.alert("evt.target" + evt.target);
							plus.nativeUI.alert(evt.target.result);
						}
						plus.nativeUI.alert(file.size + '--' + file.name);
					});
				}, function(e) {
					plus.nativeUI.alert("Resolve file URL failed: " + e.message);
				});

			},
			convertLocalFileSystemURL(){
				var path=plus.io.convertLocalFileSystemURL("/var/mobile/Containers/Data/Application/C4B37D22-B29B-4242-B400-1EBAF733BD28/Documents/Pandora/documents/test.json");
				plus.nativeUI.alert(path);

			},
			convertAbsoluteFileSystem(){
				var newurl = plus.io.convertAbsoluteFileSystem("/var/mobile/Containers/Data/Application/C4B37D22-B29B-4242-B400-1EBAF733BD28/Documents/Pandora/documents/test.json");
				plus.nativeUI.alert(newurl); // 显示值为“_document/test.json”
			},
			getAudioInfo() {
				plus.io.getAudioInfo({
					filePath:"https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-hello-uniapp/2cc220e0-c27a-11ea-9dfb-6da8e309e0d8.mp3",
					success:function(e){
						plus.nativeUI.alert(JSON.stringify(e))
					},
					fail:function(){
						plus.nativeUI.alert("fail")
					}
				});
				

			},
			getImageInfo(){
				uni.chooseImage({
					count: 1,
					sourceType: ['album'],
					success: function (res) {
						const path = res.tempFilePaths[0];
						plus.nativeUI.alert(path)
						plus.io.getImageInfo({
							src:path,
							success:function(e){
								plus.nativeUI.alert(JSON.stringify(e))
							},
							fail:function(){
								plus.nativeUI.alert("fail")
							}
						})
					}
				});
				
			},
			getVideoInfo(){
				plus.io.getVideoInfo({
					filePath:"https://www.w3school.com.cn/i/movie.ogg",
					success:function(e){
						plus.nativeUI.alert(JSON.stringify(e))
					},
					fail:function(){
						plus.nativeUI.alert("fail")
					}
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.size {
		margin: 30rpx 0;
	}
</style>
